var totalPage = 0;
var totalCnt = 0;
var chargingStationData = [];
var chargingStationDatas = [];
var branchCompanyData = [];
var chargingStationMap = undefined;
var markers = [];
var selectPosition = [];
var bulidStatusNum;
var chargingStationChart;

$(document).ready(function() {
    showLoading();
    $('title', parent.document).html($('title').text());
    setBaseUrl();
    $ajaxList('chargingStation', getChargingStation);
    $ajaxList('branchCompany', getBranchCompany);
    createTableHead("chargingStationThead", chargingStationTable, search);
    scrollTableSuspend("scrollBody", "chargingStationTable", "chargingStationTable1", "chargingStationThead", "scrollHeight");
    chargingStationChart();
    setTimeout(firstSearch,500);
    setTimeout(hideLoading, 700);
});
function firstSearch(){
	passValuesNums = window.parent.document.getElementById("passValue").value;
	  if(passValuesNums != ""){
	    $("#BulidStatus").val(parseInt(passValuesNums));
	    window.parent.document.getElementById("passValue").value = '';
	   }
	   search();
	   $ajaxList('realTimeChargingStationStatistics', getRealTimeChargingStationStatistics);
}
function resetSearch(){
	setTimeout(search, 500);
}
function getChargingStation(data) {
	var length = data.length;
	for (var i = 0; i < length; i++) {
		$("#ChargingStationID").append("<option value='" + data[i].chargingStationID + "'>" + data[i].chargingStationName + "</option>");
	}
}
function getBranchCompany(data) {
	var length = data.length;
	for (var i = 0;i < length; i++){
    	branchCompanyData = data;
        $("#BranchCompanyID").append("<option value='" + data[i].branchCompanyID + "'>" + data[i].branchCompanyName + "</option>");
    }
}
function chargingStationChart(){
	chargingStationChart = echarts.init(document.getElementById("stationEcharts"));
	chargingStationChart.setOption(searchEcharts("vertical", "center",50, 0, "统计信息", [{value: 0, name: "0", "itemStyle": echartStyle('#EAEAEA', false, false, '#EAEAEA')}]));
}
function getRealTimeChargingStationStatistics(data){
	var operation = 0; //已投运
	var construction = 0; //建设中
	var developed = 0; //待开发
	var countData = [];
	chargingStationDatas = data && data.chargingStationList || [];
	var length = chargingStationDatas.length;
	for (var i=0; i<length; i++) {
		if (chargingStationDatas[i].bulidStatus == '1') {
			developed += 1;
		} else if (chargingStationDatas[i].bulidStatus == '2') {
			construction += 1;
		} else if (chargingStationDatas[i].bulidStatus == '3') {
			operation += 1;
		}
	}
		countData.push({"value":operation, 
			"name":'已投运:' + operation, 
			"itemStyle": echartStyle('#1abc9c', true, true,'#1abc9c')
			});
		countData.push({"value":construction, 
			"name":'建设中:' + construction, 
			"itemStyle": echartStyle('#e4ae1e', true, true,'#e4ae1e')
			});
		countData.push({"value":developed, 
			"name":'规划中:' + developed, 
			"itemStyle": echartStyle('#929292', true, true,'#929292')
			});
		option.series[0].data = countData;
		option.legend.data = ['已投运:' + operation,'建设中:' + construction,'规划中:' + developed];
		chargingStationChart.setOption(option, true);
}
function search(){
	$ajax('searchChargingStation', 'searchChargingStation', searchChargingStation);
}
function searchChargingStation(data) {
	totalPage = data && data.totalPage || 0;
	totalCnt = data && data.totalCnt || 0;
	chargingStationData = data && data.chargingstationList || [];
	var length = chargingStationData.length;
	var data = [];
	$.extend(true, data, chargingStationData);
	var bulidStatus = undefined;
	for (var i = 0;i < length; i++){
		bulidStatus = chargingStationData[i].bulidStatus;
		if (bulidStatus == '1') {
			data[i].chargingStationName = '<div style="float: left"><span style="background:#929292;padding-right:15px;margin-right:5px;"></span>'+ chargingStationData[i].chargingStationName + '</div>';
		} else if (bulidStatus == '2') {
			data[i].chargingStationName = '<div style="float: left"><span style="background:#e4ae1e;padding-right:15px;margin-right:5px;"></span>' + chargingStationData[i].chargingStationName + '</div>';
		} else if (bulidStatus == '3') {
			data[i].chargingStationName = '<div style="float: left"><span style="background:#1abc9c;padding-right:15px;margin-right:5px;"></span>' + chargingStationData[i].chargingStationName + '</div>';
		} else if (bulidStatus == '0') {
			data[i].chargingStationName = '<div style="float: left"><span style="background:#3a3a3a;padding-right:15px;margin-right:5px;"></span>' + chargingStationData[i].chargingStationName + '</div>';
		}
		data[i].equipmentCount = '<a class="details" onclick=\'pass("'+chargingStationData[i].chargingStationID+'")\'>'+ ((chargingStationData[i].bgEquipmentCount || 0) + (chargingStationData[i].smEquipmentCount || 0)) +'</a>';
	}
	createTableBody("chargingStationTbody", chargingStationTable, data);
	if (length > 0) {
		$('#bulidStatusColor').empty();
		$('#bulidStatusColor').append(
				'<a onclick="bulidStatusVal(3)" class="details">已投运</a>:<span style="background:#1abc9c;padding-right:15px;margin-right:10px;"></span> ' + 
				'<a onclick="bulidStatusVal(2)" class="details">建设中</a>:<span style="background:#e4ae1e;padding-right:15px;margin-right:10px;"></span> ' + 
				'<a onclick="bulidStatusVal(1)" class="details">规划中 </a>:<span style="background:#929292;padding-right:15px;margin-right:10px;"></span>' + 
				'<a onclick="bulidStatusVal(0)" class="details">禁用</a>:<span style="background:#3a3a3a;padding-right:15px;margin-right:10px;"></span>'
			);
	}
}
function pass(value){
	window.parent.passVal(value, "passValue", "chargingEquipment/equipmentInfo", "drownDown2", "", "equipmentPage", "equipmentInfo");
}

function  bulidStatusVal(num){
	 $("#BulidStatus").val(num);
	 search();
}
function details(index){
	window.parent.detalisSkip('chargingStation/chargingStationDetail',index,$("#currentPage").val());
	$("#parentSkipMenu", parent.document).val("chargingStationPage");
	$("#detailID", parent.document).val(chargingStationData[index].chargingStationID);
}
//添加
function save() {
	window.parent.skipSecondMenu('chargingStation/chargingStationSave', 'chargingStation', 'chargingStationPage');
}
function chkBefore(index, chkFlag){
	var checkbox =document.getElementsByName("chk"); 
	for(var i=0; i<checkbox.length; i++){
		checkbox[i].checked = false; 
	}
	checkbox[index].checked = true; 
	if(chkFlag == "update"){
		update(index);
	}else if(chkFlag == "delete"){
		del();
	}
}
//修改
function update(index) {
	window.parent.detalisSkip('chargingStation/chargingStationUpdate',index,$("#currentPage").val());
	$("#parentSkipMenu", parent.document).val("chargingStationPage");
	$("#detailID", parent.document).val(chargingStationData[index].chargingStationID);
}
function confirm(){
	return checkForm("saveForm", chargingStationTable);
}
function getUrl(){
	return 'deleteChargingStation';
}
function getData(){
	return {chargingStationID: getCheckDataList(chargingStationData, "chargingStationID")};
}
function sure(data){
    var result = data && data.result;
    if (result == "Success") {
		showMessge(messageContent["Delete.Success"]);
	} else if (result == "NotFound") {
		showMessge(messageContent["Delete.NotFound"]);
	} else {
		showMessge(messageContent["Delete.Error"]);
	}
    $ajax('searchChargingStation', 'searchChargingStation', searchChargingStation);
}
